package yb.ecp.fast.user.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.constants.ActionLevel;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.user.infra.AuthCode;
import yb.ecp.fast.user.infra.BasicController;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.service.AccountService;
import yb.ecp.fast.user.service.VO.AccountPwdVO;
import yb.ecp.fast.user.service.VO.AccountVO;
import yb.ecp.fast.user.service.VO.PasswordVO;

import java.util.List;

/**
 * Created by john on 2017/10/26.
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BasicController {
    @Autowired
    AccountService accountService;

    @RequestMapping(method = RequestMethod.PUT)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult account(@RequestBody AccountVO accountVO) {
        ErrorCode ret = accountService.addAccount(accountVO);
        return actionResult(ret);
    }
    @RequestMapping(value = "/account",method = RequestMethod.DELETE)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult account(@RequestParam("loginName") String loginName){
        ErrorCode ret = accountService.removeAccount(loginName);
        return actionResult(ret);
    }

    @RequestMapping(value = "/removeByUserId",method = RequestMethod.DELETE)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult removeAccountByUserId(@RequestParam("userId") String userId){
        ErrorCode ret = accountService.removeAccountByUserId(userId);
        return actionResult(ret);
    }
    @RequestMapping(value = "/withPwd",method = RequestMethod.PUT)
    @FastMappingInfo(code = AuthCode.Account.ADD_WITH_PWD)
    public ActionResult accountPwd(@RequestBody AccountPwdVO accountPwdVO) throws Exception{
        ErrorCode ret = accountService.addAccountPwd(accountPwdVO);
        return actionResult(ret);
    }

    @RequestMapping(value = "/password",method = RequestMethod.DELETE)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult password(@RequestParam("userId") String userId)throws Exception{
        ErrorCode ret = accountService.removeAllPassword(userId);
        return actionResult(ret);
    }

    @RequestMapping(value = "/password",method = RequestMethod.PUT)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult password(@RequestBody PasswordVO passwordVO)throws Exception{
        ErrorCode ret = accountService.insertPassword(passwordVO);
        return actionResult(ret);
    }

    @RequestMapping(value = "/password",method = RequestMethod.POST)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    public ActionResult password(@RequestParam("userId") String userId, @RequestParam("password")String password)throws Exception{
        ErrorCode ret = accountService.updatePassword(userId,password);
        return actionResult(ret);
    }

    @RequestMapping(value = "/resetPassword",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Account.RESET_PASSWORD)
    public ActionResult resetPassword(@RequestBody  List<String> userIds)throws Exception{
        ErrorCode ret = accountService.resetPassword(userIds);
        return actionResult(ret);
    }

    @RequestMapping(value = "/updateLoginName",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult updateLoginName(@RequestBody AccountVO accountVO)throws Exception{
        ErrorCode ret = accountService.updateLoginName(accountVO);
        return actionResult(ret);
    }

    @RequestMapping(value = "/checkLoginName", method = RequestMethod.GET)
    public ActionResult checkLoginName(String loginName) {
        ErrorCode ret = accountService.checkLoginName(loginName);
        return actionResult(ret);
    }
    
}
